<?php
declare(strict_types=1);
namespace App\Controller\Api\V1\Paper;

use App\Controller\Api\BaseController;
use App\Repositories\PaperQuestionRepository;
use App\Repositories\PaperRepository;
use App\Repositories\UserPaperAnswerRepository;
use App\Repositories\UserPaperRepository;
use App\Request\StoreUserPaperRequest;
use App\Services\PaperService;
use Hyperf\Di\Annotation\Inject;

class PaperController extends BaseController
{
    /**
     * @Inject
     * @var PaperService
     */
    public $paper_service;

    /**
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function list(){
        $rows = $this->request->input('rows',20);
        $result = PaperRepository::getInstance()->getList('*',[],['limit'=>$rows],[[
            'field' => 'id',
            'sort' => 'desc'
        ]]);
        if(!empty($result['result'])){
            foreach($result['result'] as &$item){
                $item['published_at'] = date('m月d日',strtotime($item['created_at']));
                if(strtotime($item['start_time'])<time()&&strtotime($item['end_time'])>time()){
                    $item['is_enter']=1;
                }else{
                    $item['is_enter']=0;
                }
            }
        }
        return $this->response->success($result);
    }

    public function detail(){
        $id = $this->request->input('id',0);
        $paper = PaperRepository::getInstance()->getById($id);
        if(!$paper){
            return $this->response->error(100404,'问卷不存在');
        }
        $questions = PaperQuestionRepository::getInstance()->all('*',[[
            'field' => 'paper_id',
            'value' => $id
        ]],[[
            'field'=>'listorder',
            'sort' => 'asc'
        ],[
            'field'=>'id',
            'sort'=>'asc'
        ]]);
        $paper['questions'] = $questions;
        return $this->response->success($paper);
    }

    public function submit(StoreUserPaperRequest $request){
        $params = $request->all();
        if(is_null(json_decode($params['questions']))){
            return $this->response->error(10050,'请上传答题数据');
        }
        $user = $this->request->getAttribute('user');
        $result = $this->paper_service->submit($params,$user);
        if(!$result['success']){
            return $this->response->error(10050,$result['msg']);
        }
        return $this->response->success($result['data']);
    }

    public function mine(){
        $rows = $this->request->input('rows',20);
        $user = $this->request->getAttribute('user');
        $where=[[
            'field'=>'user_id',
            'value' => $user->id
        ]];
        $result = UserPaperRepository::getInstance()->getList('*',$where,['limit'=>$rows],[[
            'field' => 'id',
            'sort' => 'desc'
        ]]);
        if(!empty($result['result'])){
            foreach($result['result'] as &$item){
                $item['paper'] = PaperRepository::getInstance()->getById($item['paper_id']);
            }
        }
        return $this->response->success($result);
    }

    public function answerDetail(){
        $user_paper_id = $this->request->input('user_paper_id',0);
        $user_paper = UserPaperRepository::getInstance()->getById($user_paper_id);
        $paper = PaperRepository::getInstance()->getById($user_paper->paper_id);
        $answers = UserPaperAnswerRepository::getInstance()->all('*',[[
            'field' => 'user_paper_id',
            'value' => $user_paper_id
        ]]);
        foreach ($answers as &$item){
            $item->question_answers = json_decode($item->question_answers,true);
        }
        $user_paper->questions = $answers;
        $user_paper->paper = $paper;
        return $this->response->success($user_paper);
    }

}